from fastapi import FastAPI, Response, Request
from fastapi.responses import PlainTextResponse, HTMLResponse

app = FastAPI()

@app.get("/")
async def main(request: Request):
    page_count = request.cookies.get("page_count")
    if page_count is None:
        page_count = 0
        resp = PlainTextResponse(content=f"Count={page_count}，新Cookie")
        resp.set_cookie(key="page_count", value=str(page_count))
        return resp

    page_count = int(page_count) + 1

    resp = PlainTextResponse(content=f"Count={page_count}")
    resp.set_cookie(key="page_count", value=str(page_count))
    return resp

@app.get("/del")
async def del_cookie():
    resp = HTMLResponse(content="""
    <html><body>
    <h4><a href="/">返回根页面</a></h4>
    删除 page_count Cookie.
    </body></html>
    """, status_code=200)
    resp.delete_cookie("page_count")
    return resp
